//
//  ProductDetailViewController.m
//  LoveTrail
//
//  Created by giantumbrella on 15/6/10.
//  Copyright (c) 2015年 赵娟. All rights reserved.
//

#import "ProductDetailViewController.h"
#import "CommonMacro.h"
#import "SubmitOrderVC.h"
#import "AddBuyCarViewController.h"

#import "ProductImgOtherCell.h"
#import "PDetailProductNumCell.h"
#import "ProductGategoryCell.h"
#import "AccountNumCell.h"
#import "ProductCommentCell.h"

#import "ProductImgOtherModel.h"
#import "PDeProNumModel.h"

#import "ProductDetailWebView.h"
#import "CommentVIew.h"
static int num = 1;
@interface ProductDetailViewController ()<helpAddAndReductionDelegate,clickButtonChangeContentDelegate,changButtonStatusDelegate>

@property (nonatomic,strong) NSArray *faulseArray1;
@property (nonatomic,strong) NSArray *faulseArray2;
@property (nonatomic,strong) ProductDetailWebView *productDetailWebView;
@property (nonatomic,strong) CommentVIew *commentView;
@property (nonatomic,strong) ProductCommentCell *productCommentCell ;

@end

@implementation ProductDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tempArr = [[NSMutableArray alloc] init];
    self.contentArr = [[NSMutableArray alloc] init];
    
    //调用数据源部分
    [self createFaulseData];
    
    self.title = @"商品详情";
    self.view.backgroundColor = UIColorWithRGBA(238, 238, 238, 1);
    
    //创建talbelView
    [self createTableView];
    
    //加入购物车
    [self createBuyNowAndAddBuyCar];
}

#pragma mark  跳入 就将要显示
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 商品详情网页
    self.productDetailWebView = [[ProductDetailWebView alloc] initWithFrame:CGRectMake(0, self.tableView.frame.size.height+self.tableView.frame.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT*0.7)];
    [self.scrollView addSubview:self.productDetailWebView];
}

#pragma mark 创建数据源部分
- (void)createFaulseData
{
    //添加数据源  假 这是第一个cell的数据
    self.faulseArray1 = [[NSArray alloc] init];
    self.faulseArray1 = @[@{@"imageURL":@"http://www.mmisy.com/data/upload/shop/store/goods/1/1_04856917832855434_240.jpg",@"title":@"原产美国雅培精装婴幼儿奶粉试用",@"specification":@"900克",@"useAge":@"1-3岁",@"amount":@"354",@"price":@"￥396",@"stock":@"2000"}];
    ProductImgOtherModel *productImgOtherModel = [[ProductImgOtherModel alloc] init];
    [productImgOtherModel decodeDic:self.faulseArray1[0]];
    [self.contentArr addObject:productImgOtherModel];
    
//    for (NSDictionary *key in self.faulseArray1) {
//        NSLog(@"%@",key);
//        
//    }

for (NSDictionary *key in self.faulseArray1) {
    for (NSString *value in key) {
        [self.tempArr addObject:key[@"imageURL"]];
        [self.tempArr addObject:key[@"title"]];
        [self.tempArr addObject:key[@"price"]];
        NSLog(@"%@",value);
    }
  }
    self.faulseArray2 = [[NSArray alloc] init];
    self.faulseArray2 = @[@{@"productNum":@"配料表：糙米粉、大米粉、浓缩苹果汁、碳酸钙、碳酸三钙、碳酸氢钙、酒石酸清单件、焦磷酸铁（铁）、L-抗坏血酸钠(维生素C)、盐酸氨（维生素B3）、d-a-醋酸生育酚（维生素E）、氧化锌等",@"shelfLif":@"540",@"specification":@"60g*4",@"methord":@"常温阴凉干燥存放、避免阳光直射。",@"typeOfPackage":@"灌装",@"probationAge":@"9个月以上"}];
    PDeProNumModel *pDetailProductNumModel = [[PDeProNumModel alloc] init];
    [pDetailProductNumModel decodeDic:self.faulseArray2[0]];
    [self.contentArr addObject:pDetailProductNumModel];
}

#pragma mark 创建一个tableView
- (void)createTableView
{
    //添加scrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+SCREEN_HEIGHT);
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    //添加tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*1.2)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.scrollView addSubview:self.tableView];
}


#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID1 = @"Cell1";
    static NSString *ID2 = @"Cell2";
    static NSString *ID3 =  @"Cell3";
    static NSString *ID4 = @"Cell4";
    static NSString *ID5 = @"Cell5";
    ProductImgOtherCell *cell1 = [tableView dequeueReusableCellWithIdentifier:ID1];
    PDetailProductNumCell *cell2 = [tableView dequeueReusableCellWithIdentifier:ID2];
    ProductGategoryCell *cell3 = [tableView dequeueReusableCellWithIdentifier:ID3];
    AccountNumCell *cell4 = [tableView dequeueReusableCellWithIdentifier:ID4];
    ProductCommentCell *cell5 = [tableView dequeueReusableCellWithIdentifier:ID5];
    switch (indexPath.section) {
        case 0:
            if (cell1==nil)
            {
                cell1 = [[ProductImgOtherCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID1];
                cell1.productImgOtherModel = self.contentArr[0];
                cell1.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            return cell1;
            
            break;
            
        case 1:
            if (cell2==nil)
            {
                cell2 = [[PDetailProductNumCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID1];
                PDeProNumModel *model = self.contentArr[1];
                cell2.proDetailProNumModel = model;
            }
               cell2.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell2;
            break;
        case 2:
            if (cell3==nil)
            {
                cell3 = [[ProductGategoryCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID1];
            }
            [cell3.buttonCao setTitle:@"草莓味" forState:UIControlStateNormal];
            [cell3.buttonApple setTitle:@"苹果味" forState:UIControlStateNormal];
            [cell3.buttonBannar setTitle:@"香蕉味" forState:UIControlStateNormal];
            [cell3.buttonFan setTitle:@"番茄味" forState:UIControlStateNormal];
               cell3.selectionStyle = UITableViewCellSelectionStyleNone;
            cell3.delegate = self;
            return cell3;
            break;
            
        case 3:
            if (cell4==nil)
            {
                cell4 = [[AccountNumCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID1];
            }
            cell4.delegate = self;
            cell4.selectionStyle = UITableViewCellSelectionStyleNone;
            cell4.totalNumLabel.text = [NSString stringWithFormat:@"%d",num];
            
            [[NSUserDefaults standardUserDefaults] setObject:cell4.totalNumLabel.text forKey:@"totalNum"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
//            NSString *userId = _phoneNum;
//            NSString *userCode = _passWord.text;
//            [[NSUserDefaults standardUserDefaults] setObject:userId forKey:@"userId"];
//            [[NSUserDefaults standardUserDefaults] setObject:userCode forKey:@"userCode"];
            return cell4;
            break;
            
        case 4:
            if (cell5==nil)
            {
                cell5 = [[ProductCommentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID1];
            }
               cell5.selectionStyle = UITableViewCellSelectionStyleNone;
            cell5.delegate = self;
               return cell5;
            break;
            
        default:
            break;
    }
    return cell1;
    return cell2;
    return cell3;
    return cell4;
    return cell5;
}

#pragma mark 头部大图片
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *viewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 140)];
    if (section==0) {
        
        viewTop.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"头像背景"]];
    }
    return viewTop;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0 || indexPath.section==1) {
        return 160;
    }
    else if (indexPath.section==2 || indexPath.section==3){
        return 75;
    }
    else
    {
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 140;
    }
    else
    {
        return 10;
    }
}

#pragma mark 添加底部的view视图 用于显示 商品详情  商品评论
- (void)addButtomView
{
    //商品评价
    self.commentView = [[CommentVIew alloc] initWithFrame:CGRectMake(0,self.tableView.frame.size.height+self.tableView.frame.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT*0.7)];
    [self.scrollView addSubview:self.commentView];
}

#pragma mark 购买数量 加减操作
- (void)helpAddAndReduction:(UIButton *)button
{
    if (button.tag==1000) {
        if (num>0) {
            num--;
        }
        else
        {
            num=0;
        }
    }
    else
    {
        num++;
    }
    [self.tableView reloadData];
}

#pragma mark clickButtonChangeContentDelegate 商品详情  商品评价
- (void)clickButtonChangeContent:(UIButton *)button
{
    NSLog(@"aclick");
    self.productCommentCell = [[ProductCommentCell alloc] init];
    
    if (self.productCommentCell.lastButton!=button) {
        self.productCommentCell.lastButton.selected=NO;
    }
    self.productCommentCell.lastButton=button;
    button.selected=YES;
 
//        if (button.tag==1000) {
//    
//            self.productCommentCell.commentProductButton.selected = NO;
//            self.productCommentCell.productDetailButton.selected = YES;
//    
//       }else
//       {
//           self.productCommentCell.commentProductButton.selected = YES;
//           self.productCommentCell.productDetailButton.selected = NO;
//       }
    
    //添加底部视图 暂时写法 通过点击按钮实现
    [self addButtomView];
    if (button.tag==1000) {
        [self.scrollView bringSubviewToFront:self.productDetailWebView];
    }
    else{
        [self.scrollView bringSubviewToFront:self.commentView];
    }

}

#pragma mark  商品分类 选中 颜色 or 味道
 - (void)changButtonStatus:(UIButton *)button
{
    NSLog(@" 商品分类 选中 颜色 or 味道");
}

#pragma mark 添加 立即购买  加入购物车
- (void)createBuyNowAndAddBuyCar
{
    UIButton *nowBuyButton = [[UIButton alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT*0.94, self.tableView.frame.size.width*0.5-2, SCREEN_HEIGHT*0.2)];
    [nowBuyButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [nowBuyButton setTitleColor:UIColorWithRGBA(254, 241, 238, 1) forState:UIControlStateNormal];
    [nowBuyButton setBackgroundImage:[UIImage imageNamed:@"提交按钮"] forState:UIControlStateNormal];
    [self.view addSubview:nowBuyButton];
    [nowBuyButton addTarget:self action:@selector(buyNowGo) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *addBuyCarButton = [[UIButton alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width*0.5+2,SCREEN_HEIGHT*0.94, self.tableView.frame.size.width*0.5-2, SCREEN_HEIGHT*0.2)];
    [addBuyCarButton setTitle:@"加入购物车" forState:UIControlStateNormal];
    
    [addBuyCarButton setBackgroundImage:[UIImage imageNamed:@"提交按钮"] forState:UIControlStateNormal];
    [addBuyCarButton setImage:[UIImage imageNamed:@"_0001_购物车@2x"] forState:UIControlStateNormal];
    [addBuyCarButton setTitleColor:UIColorWithRGBA(254, 241, 238, 1) forState:UIControlStateNormal];
    [self.view addSubview:addBuyCarButton];
    [addBuyCarButton addTarget:self action:@selector(addBuyCar) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark 立即购买  加入购物车
- (void)buyNowGo
{
    SubmitOrderVC *submitOrder = [[SubmitOrderVC alloc] init];
    UINavigationController *submitOrderNav = [[UINavigationController alloc] initWithRootViewController:submitOrder];
    [self presentViewController:submitOrderNav animated:YES completion:nil];}

- (void)addBuyCar
{
    AddBuyCarViewController *addBuy = [[AddBuyCarViewController alloc]init];
    UINavigationController *addBuyNav = [[UINavigationController alloc] initWithRootViewController:addBuy];
    addBuy.pathArray = self.faulseArray1;
    [self presentViewController:addBuyNav animated:YES completion:nil];
}

@end
